package com.tinem.platform.web.auth.controller;

import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@SuppressWarnings("unchecked")
@SessionAttributes("authorizationRequest")
public class BootGrantController {

    @RequestMapping("/access.do")
    public ModelAndView getAccessConfirmation(Map<String, Object> model, HttpServletRequest request) throws Exception {
        AuthorizationRequest authorizationRequest = (AuthorizationRequest) model.get("authorizationRequest");
        ModelAndView view = new ModelAndView();
        view.setViewName("access");
        view.addObject("clientId", authorizationRequest.getClientId());

        Map<String, String> scopes = (Map<String, String>) (model.containsKey("scopes") ?
            model.get("scopes") : request.getAttribute("scopes"));
        view.addObject("scopes", scopes.keySet());
        return view;
    }

    @RequestMapping("login.do")
    public String login(HttpServletRequest request) throws Exception {
        return "login";
    }
}
